# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jeff Bilyk <jbilyk@gmail.com>
pkgname=nagios-plugins
pkgver=2.3.2
pkgrel=0
pkgdesc="Plugins for Nagios to check services on hosts"
url="https://nagios-plugins.org"
arch="all"
license="GPL-3.0-only"
_depends_by_ssh="openssh-client"
_depends_dig="bind-tools"
_depends_disk_smb="samba-client"
_depends_dns="bind-tools"
_depends_fping="fping"
_depends_log="grep"
_depends_rcp="rpcbind"
_depends_sensors="grep lm-sensors"
_depends_snmp="net-snmp-tools"
makedepends="openssh-client bind-tools samba-client fping grep rpcbind
	lm-sensors net-snmp-tools
	file freeradius-client-dev libdbi-dev libpq linux-headers mariadb-dev
	mariadb-connector-c-dev perl
	net-snmp-dev openldap-dev opensmtpd openssl-dev postgresql-dev"
install="$pkgname.post-upgrade"
pkgusers="nagios"
pkggroups="nagios"
options="suid"
source="https://www.nagios-plugins.org/download/nagios-plugins-$pkgver.tar.gz
	fix-mysql-headers.patch
	check_openrc
	nagios-openrc.sudoers"

_plugins_dir="/usr/lib/nagios/plugins"

# Note: Split function takes $name*, so ntp must be before nt etc.
_plugins="breeze by_ssh cluster dbi dhcp dig disk_smb disk dns dummy file_age
	fping hpjd http icmp ide_smart ifoperstatus ifstatus ircd ldap load log
	mailq mrtgtraf mrtg mysql nagios ntp nt nwstat overcr pgsql ping
	procs radius real rpc sensors smtp snmp ssh swap time ups uptime users
	wave ssl_validity"
for _sub in $_plugins; do
	subpackages="$subpackages $pkgname-$_sub:_plugin"
done
subpackages="$subpackages
	$pkgname-openrc:_openrc:noarch
	$pkgname-tcp
	$pkgname-all:_all"  # all must be the last
_plugins="$_plugins openrc tcp"


prepare() {
	default_prepare
	update_config_sub
}

build() {

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--bindir=/usr/sbin \
		--libexecdir="/$_plugins_dir" \
		--localstatedir=/var/nagios \
		--with-ipv6 \
		--with-ps-command="/bin/ps -o stat,pid,ppid,vsz,rss,comm,args" \
		--with-ps-format="%s %d %d %d %d %s %n" \
		--with-ps-varlist="procstat,&procpid,&procppid,&procvsz,&procrss,procprog,&pos" \
		--with-ps-cols=7 \
		--with-ping-command="/bin/ping -c %d %s" \
		--disable-nls
	make all
}

package() {

	make DESTDIR="$pkgdir" install

	# Remove plugins that are useless or doesn't work on Alpine.
	local name; for name in apt flexlm oracle; do
		rm "$pkgdir/$_plugins_dir"/check_$name
	done

	rm -rf "$pkgdir"/usr/lib/charset.alias
	rmdir -p "$pkgdir"/usr/lib "$pkgdir"/usr/include 2>/dev/null || true
}

tcp() {
	local plugins=" clamd ftp imap jabber nntp nntps pop simap spop ssmtp udp"
	local destdir="$subpkgdir/$_plugins_dir"
	pkgdesc="Nagios plugins check_tcp${plugins// /, check_}"
	provides="${plugins// /$pkgname-}"

	mkdir -p "$destdir"
	local name; for name in tcp $plugins; do
		mv "$pkgdir/$_plugins_dir"/check_$name "$destdir"/
	done
}

_openrc() {
	pkgdesc="Nagios plugin check_openrc"
	depends="$pkgname openrc sudo"

	install -m 755 -D "$srcdir"/check_openrc \
		"$subpkgdir"/$_plugins_dir/check_openrc
	install -m 600 -D "$srcdir"/nagios-openrc.sudoers \
		"$subpkgdir"/etc/sudoers.d/nagios-openrc
}

_plugin() {
	local name="${subpkgname#$pkgname-}"
	local destdir="$subpkgdir/$_plugins_dir"
	pkgdesc="Nagios plugin check_$name"
	depends="$pkgname $(eval "echo \$_depends_$name")"

	mkdir -p "$destdir"
	mv "$pkgdir/$_plugins_dir"/check_$name* "$destdir"/

	if file -bi "$destdir"/* | grep -qw text/x-perl; then
		depends="$depends perl"
	fi
#	if ! file -bi "$destdir"/* | grep -qw application/x-sharedlib; then
#		arch="noarch"
#	fi
}

_all() {
	pkgdesc="Metapackage that installs *all* Nagios plugins from the nagios-plugins package"

	depends=''
	local plugin; for plugin in $_plugins; do
		depends="$depends $pkgname-$plugin"
	done

	mkdir -p "$subpkgdir"

	# We exploit the fact that the -all subpackage is created last
	# and check if all the plugins have been moved to subpackages.
	local path= retval=0
	for path in "$pkgdir/$_plugins_dir"/check_*; do
		[ -r "$path" ] || continue
		error "Please create a subpackage for ${path##*/} or remove it"
		retval=1
	done
	return $retval
}

sha512sums="c73e13800ad408f9b345fbfc2142ae2275b77e4865ecdb9be33a86024c10be1e54e401450c3f7c848e96bd1152c723426477a1f2874e3bf5dd149f07391b1dd9  nagios-plugins-2.3.2.tar.gz
5db87cf63fb6d1f72122bd7c72f012aab2724328d3d71da546970792a6cdeedbe008f3feef03f7432f100836ace3701c1902b820c93a79e5fa13abe8b337da0d  fix-mysql-headers.patch
12d87542631494df1c961e547c19107a025829509e174e8208111736141c12e20dbf490c55d487af39d47cefca5507cd98c973b7b20ae3f961dcbfd167195d8e  check_openrc
171c9ad14d1027541b78df76063e6d34483dd536219fb83e0346e191739529d59c8d6be468af7f1c4c93b20baf9a32879510f15ec3d06aa1eefaf5d785ea8546  nagios-openrc.sudoers"
